package cacco.jp.mld.panel.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cacco.jp.mld.panel.entity.LogEntity;

@Repository
public class LogDao {
  
	private static int totalRecords;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public int add(LogEntity log) {
		int id = log.getId();
	    this.sessionFactory.getCurrentSession().save(log);
	    id = log.getId();
	    return id;
	}
	
	@Transactional
	public List<LogEntity> getAll() {
	    return this.sessionFactory.getCurrentSession().createQuery("from LogEntity").list();
   }

	@Transactional
	public void delete(Integer id) {
	    LogEntity log = (LogEntity) sessionFactory.getCurrentSession().load(
	            LogEntity.class, id);
	    if (null != log) {
	        this.sessionFactory.getCurrentSession().delete(log);
	    }
	}
	@Transactional
	public LogEntity findById(int id) {
        Query query =  sessionFactory.getCurrentSession().getNamedQuery("findLogById");
        query.setInteger("id", id);
        LogEntity logObj = (LogEntity) query.uniqueResult();
        return logObj;
    }   
	@Transactional
	public void update(LogEntity log){
		sessionFactory.getCurrentSession().update(log);
	}
	@Transactional
    public List<LogEntity> getAlByPage(int pageSize, int page, String sidx, String sord) {
        List<LogEntity> list = null;
        Query query = sessionFactory.getCurrentSession().createQuery("from LogEntity order by "+sidx+" "+sord);
        totalRecords = query.list().size();
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);
        list = (List<LogEntity>)query.list();
        return list;
    }

	public static int getTotalRecords() {
		return totalRecords;
	}
	
}
